﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;
using System.Windows.Forms;

namespace Deduplicator
{
    class FileCounter
    {
        public int MusicFiles = 0;
        public Int64 MusicSize = 0;

        public int PictureFiles = 0;
        public Int64 PicturesSize = 0;

        //public DataColumn MovieFilesC;
        public int MovieFiles = 0;
        public Int64 MovieSize = 0;

        public int TotalFileCount = 0;
        public Int64 TotalSize = 0;

        public int MovedFiles = 0;
        public int DuplicateFiles = 0;

        public int InAccessable = 0;

        private TextBox lblmusic;
        private TextBox lblmovie;
        private TextBox lblpicture;
        private TextBox lblfileCount;
        private PictureBox pictureBox;

        public FileCounter(TextBox lblMusic, TextBox lblMovie, TextBox lblPicture, TextBox lblFileCount, PictureBox picturebox)
        {
            lblmusic = lblMusic;
            lblmovie = lblMovie;
            lblpicture = lblPicture;
            lblfileCount = lblFileCount;
            pictureBox = picturebox;
        }

        public void MoveFile(string fileName, string DestinationFolder)
        {
            string Ext = Path.GetExtension(fileName).ToLower();
            //if (IsPictureFile(Ext))
            if (IsMovieFile(Ext))
            {
                try
                {
                    TotalFileCount++;

                    if (TotalFileCount % 25 == 0)
                    {
                        lblpicture.Text = "Moved: \t" + MovedFiles + "\t" + " Duplicates: \t" + DuplicateFiles;
                        lblfileCount.Text = TotalFileCount.ToString();
                        lblmusic.Parent.Parent.Parent.Update();
                    }

                    DateTime creationtime = File.GetCreationTime(fileName);
                    DateTime lastwritetime = File.GetLastWriteTime(fileName);

                    if (lastwritetime < creationtime)
                        creationtime = lastwritetime;

                    string DestinationDirectory = DestinationFolder + "\\" + creationtime.Year + "-" + creationtime.Month + "\\";
                    string DestinationFileName = DestinationDirectory + Path.GetFileName(fileName);

                    if (File.Exists(DestinationFileName))
                    {
                        DuplicateFiles++;
                        DestinationDirectory = DestinationFolder + "\\Duplicates\\";
                        DestinationFileName = DestinationDirectory + Path.GetFileName(fileName);
                    }
                    else
                        MovedFiles++;

                    if (Directory.Exists(DestinationDirectory) == false)
                        Directory.CreateDirectory(DestinationDirectory);

                    if (File.Exists(DestinationFileName))
                        File.Delete(DestinationFileName);

                    File.Move(fileName, DestinationFileName);
                }
                catch
                {
                    InAccessable += 1;
                    // lblmemo.Text = "Cannot scan " + fileName;
                    //lblmemo.Update();
                }
            }

        }

        bool IsPictureFile(string fileExtension)
        {
            bool isPicture = fileExtension == ".jpg" || fileExtension == ".png";
            if (isPicture == false)
                isPicture = fileExtension == ".bmp" || fileExtension == ".tif";
            if (isPicture == false)
                isPicture = fileExtension == ".psd";
            return isPicture;
        }

        bool IsMusicFile(string fileExtension)
        {
            return fileExtension == ".mp3";
        }

        bool IsMovieFile(string fileExtension)
        {
            bool isMovie = fileExtension == ".avi" || fileExtension == ".3gp";
            if (isMovie == false)
                isMovie = fileExtension == ".mts" || fileExtension == ".mov";
            if (isMovie == false)
                isMovie = fileExtension == ".mpg" || fileExtension == ".mp4";
            if (isMovie == false)
                isMovie = fileExtension == ".wmv"; 
            return isMovie;
        }


        public void AddFile(string FileName)
        {
            FileInfo fi = new FileInfo(FileName);
            TotalFileCount++;
            int Size = (int)fi.Length;
            TotalSize += Size;

            if (TotalFileCount % 250 == 0)
            {
                lblmusic.Text = MusicFiles.ToString() + " Files\t" + (MusicSize / (1024 * 1024)) + " Mb";
                lblmovie.Text = MovieFiles.ToString() + " Files\t" + (MovieSize / (1024 * 1024)) + " Mb"; ;
                lblpicture.Text = PictureFiles.ToString() + " Files\t" + (PicturesSize / (1024 * 1024)) + " Mb";
                lblfileCount.Text = TotalFileCount.ToString() + " Files\t" + (TotalSize / (1024 * 1024)) + " Mb";
                lblmusic.Parent.Parent.Parent.Update();
                DrawThumbnail(FileName);
            }

            string Ext = Path.GetExtension(FileName).ToLower();
            if (IsPictureFile(Ext))
            {
                PictureFiles++;
                PicturesSize += Size;
            }
            else if (IsMusicFile(Ext))
            {
                MusicFiles++;
                MusicSize += Size;
            }
            else if (IsMovieFile(Ext))
            {
                MovieFiles++;
                MovieSize += Size;
            }
        }


        public bool ThumbnailCallback()
        {
            return false;
        }

        public void DrawThumbnail(string fileName)
        {
            FileStream f = new FileStream(fileName, FileMode.Open);
            try
            {
                Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
                Bitmap myBitmap = new Bitmap(f);
                pictureBox.Image = myBitmap.GetThumbnailImage(128, 128, myCallback, IntPtr.Zero);
                pictureBox.Image.GetThumbnailImage(128, 128, myCallback, IntPtr.Zero);
                pictureBox.Update();
            }
            finally
            {
                f.Dispose();
            }
        }
    }
}
